package home

import (
	"gitee.com/johng/gf/g"
	"gitee.com/johng/gf/g/crypto/gmd5"
	"gitee.com/johng/gf/g/frame/gins"
	"gitee.com/johng/gf/g/net/ghttp"
)

type Home struct{}

type resp struct {
	Success int         `json:"success"`
	Msg     interface{} `json:"msg"`
}

var DB = g.DB()

func init() {
	g.Server().BindObject("/home", new(Home))
}

func (h *Home) Index(r *ghttp.Request) {
	content, _ := gins.View().Parse("index.html", map[string]interface{}{})
	r.Response.Write(content)
}

/*
	管理员登录验证
*/
func (h *Home) Login(r *ghttp.Request) {
	cfg := g.Config()
	params := r.GetRequestMap()
	if params == nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: "未获取到参数,请重试！"})
		return
	}
	if params["username"] != cfg.GetString("server_user") || gmd5.Encrypt(gmd5.Encrypt(params["password"])) != cfg.GetString("server_pwd") {
		r.Response.WriteJson(&resp{Success: 2, Msg: "用户名或密码错误！"})
		return
	} else {
		r.Session.Set("admin", gmd5.Encrypt(gmd5.Encrypt(params["password"])))
		r.Response.WriteJson(&resp{Success: 0, Msg: "登录成功!"})
	}
}

/*
	获取会员数量
*/
func (h *Home) MemberCount(r *ghttp.Request) {

	res, err := DB.Table("es_member").Count()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(&resp{Success: 0, Msg: res})
}

/*
	获取会员列表
*/
func (h *Home) MemberList(r *ghttp.Request) {

	res, err := DB.Table("es_member").All()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}

/*
	获取备份服务器数量
*/
func (h *Home) HostCount(r *ghttp.Request) {

	res, err := DB.Table("es_backup_server").Count()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(&resp{Success: 0, Msg: res})
}

/*
	获取备份服务器列表
*/
func (h *Home) HostList(r *ghttp.Request) {
	dbmodel := DB.Table("es_backup_server as bs").InnerJoin("es_member as member", "bs.memberuuid = member.uuid").Fields("bs.*,FROM_UNIXTIME(bs.create_time,'%Y-%m-%d %H:%i:%s') as create_time,member.nickname,member.realname")
	res, err := dbmodel.OrderBy("create_time desc").All()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}

/*
	获取订单数量
*/
func (h *Home) OrderCount(r *ghttp.Request) {

	res, err := DB.Table("es_order").Fields("count(*) as count,mark").GroupBy("mark").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(&resp{Success: 0, Msg: res.ToList()})
}

/*
	获取订单列表
	更新gf框架后需要dbmodel重新赋值了，不能直接使用。
*/
func (h *Home) OrderList(r *ghttp.Request) {

	dbmodel := DB.Table("es_order as orders").InnerJoin("es_member as member", "orders.memberuuid = member.uuid").Fields("orders.*,FROM_UNIXTIME(orders.create_time,'%Y-%m-%d %H:%i:%s') as create_time,member.nickname,member.realname")
	if r.Get("mark") != "" {
		dbmodel.Where("mark=?", r.Get("mark"))
	}

	res, err := dbmodel.OrderBy("create_time desc").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}

/*
	获取服务器下数据库
*/
func (h *Home) DbsList(r *ghttp.Request) {

	dbmodel := DB.Table("es_backup_database as dbs").InnerJoin("es_member as member", "dbs.memberuuid = member.uuid").Fields("dbs.*,FROM_UNIXTIME(dbs.create_time,'%Y-%m-%d %H:%i:%s') as create_time,member.nickname,member.realname")
	if r.Get("server_id") != "" {
		dbmodel.Where("server_id=?", r.Get("server_id"))
	}

	res, err := dbmodel.OrderBy("create_time desc").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}

/*
	获取数据库下备份文件
*/
func (h *Home) DbfList(r *ghttp.Request) {

	dbmodel := DB.Table("es_backup_file as files").Fields("files.*,FROM_UNIXTIME(files.create_time,'%Y-%m-%d %H:%i:%s') as create_time")
	if r.Get("database_id") != "" {
		dbmodel.Where("database_id=?", r.Get("database_id"))
	}

	res, err := dbmodel.OrderBy("create_time desc").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}

/*
	分享链接记录
*/

func (h *Home) InviteCount(r *ghttp.Request) {

	res, err := DB.Table("es_inviter_log").Fields("count(*) as count,mark").GroupBy("mark").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(&resp{Success: 0, Msg: res.ToList()})
}

/*
	分享链接列表
*/
func (h *Home) InviteList(r *ghttp.Request) {

	dbmodel := DB.Table("es_inviter_log as inviter").InnerJoin("es_member as member", "inviter.inviter_uuid = member.uuid").Fields("inviter.*,inviter.memberuuid as inviteduuid,FROM_UNIXTIME(inviter.create_time,'%Y-%m-%d %H:%i:%s') as create_time,member.nickname as inviter_name,member.realname as inviter_realname")
	if r.Get("mark") != "" {
		dbmodel.Where("mark=?", r.Get("mark"))
	}
	res, err := dbmodel.OrderBy("create_time desc").Select()
	if err != nil {
		r.Response.WriteJson(&resp{Success: 2, Msg: err.Error()})
		return
	}
	r.Response.WriteJson(res.ToList())
}
